package com.steakliu.sun.ratelimiter;

import com.google.common.util.concurrent.RateLimiter;
import com.steakliu.sun.ratelimiter.api.AbstractRateLimiter;
import com.steakliu.sun.ratelimiter.api.SunRateLimiter;
import com.steakliu.sun.spi.annotation.Join;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2023-02-07  15:36
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
@Join
public class GuavaRateLimiter extends AbstractRateLimiter {

    private final Logger LOGGER = LoggerFactory.getLogger(GuavaRateLimiter.class);

    private RateLimiter rateLimiter;

    @Override
    public boolean tryAcquire() {
        return rateLimiter.tryAcquire();
    }

    @Override
    public void release() {

    }

    @Override
    public void init(int milliseconds, int permits) {
        super.init(milliseconds,permits);
        /**
         *  milliseconds: 1000
         *  permits: 100
         *
         *  一秒钟 100个资源
         */
        int ps = (milliseconds / 1000) * permits;
        this.rateLimiter = RateLimiter.create(ps);
    }
}
